package com.say.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.say.bean.common.Response;
import com.say.bean.domain.Trace;
import com.say.bean.domain.User;
import com.say.service.TraceService;
import com.say.service.UserService;
import com.say.utils.CookiesUtil;
import com.say.utils.ImageTools;
import com.say.utils.MD5;
import com.say.utils.UserUtil;

@Controller
@RequestMapping()
public class PublicController extends BaseController {

    @Autowired
    protected UserService userService;
    
    @Autowired
    protected TraceService traceService;
    
	@RequestMapping("/")
	public String index(HttpServletRequest request,Model model){
		User user = UserUtil.getUser(request);
		if(user==null) {
			return "redirect:/login.html";
		}else {
			return "redirect:/wensay/home.html";
		}
	}
	
	
	@RequestMapping("/login")
	public String login(HttpServletRequest request,Model model){
		request.getSession().removeAttribute("user");
		request.getSession().removeAttribute("userId");
		CookiesUtil.setCookie(response, "_t", "", 1);
		return "common/login";
	}
	
	
	@RequestMapping(value = "/login/post")
    @ResponseBody
    public Response postLogin(HttpServletResponse res,String name,String password) throws Exception {
		Response response = new  Response();
		User user=userService.getUserByUserName(name);
		if(user==null) {
			response.failure("用户名不存在");
	        return response;
		}else {
			if(user.getPassword().equals(MD5.getMD5(password.getBytes()))) {
				request.getSession().setAttribute("user",user);
				request.getSession().setAttribute("userId",user.getId());
				CookiesUtil.setCookie(res, "_t", user.getId(), 3600000);
				response.success("登录成功");
				
				traceService.save(new Trace(user.getId(),"0.0.0.0","请求登录接口"));
				return response;
			}else {
				response.failure("密码错误");
				return response;
			}
		}
    }

	public static void main(String[] args) {
		
		System.out.println(MD5.getMD5("812929".getBytes()));
		
	}
	
	@RequestMapping(value = "/file/upload")
    @ResponseBody
    public Response upload4Base64(@RequestParam(name = "pic") String pic) throws Exception {
		Response response = new  Response();
        if (StringUtils.isBlank(pic)) {
        	response.failure("上传图片失败");
            return response;
        }
        String filePath = ImageTools.savePic4Base64(pic);
        if (StringUtils.isNotBlank(filePath)) {
        	Map<String, Object> data = new HashMap<String, Object>();
        	data.put("filePath", filePath);
        	response.success(data,"上传图片成功 ");
        } else {
        	response.failure("上传图片失败");
        }
        return response;
    }

	


}
